﻿using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Controls;
using System.Threading;

using SBPweb.PROvibe.Shared.Contracts.Content;

using SBPweb.PROvibe.Client.Framework.Controls;
using SBPweb.PROvibe.Client.Framework.Contracts.Content;
using SBPweb.PROvibe.Client.Framework.Utilities.Events;

namespace SBPweb.PROvibe.Client.Modules.Controls
{

    public partial class Login : SmartpartControl
    {

        #region Constructor logic

        public Login()
            : base()
        {
            InitializeComponent();
            InitializeControls();
        }

        public Login(IPage parentPage)
            : base(parentPage)
        {
            InitializeComponent();
            InitializeControls();
        }

        protected override void InitializeControls()
        {
            Caption = "Login";
            if (ParentPage.ContentManager.Session.ContainsKey("userName"))
            {
                tbUsername.Text = ParentPage.ContentManager.Session.Get<string>("userName");
            }
        }

        #endregion

        #region Property accessors

        #endregion

        #region UI event handlers

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (IsInContainer)
            {
                Container.Hide();
            }
            string userName = tbUsername.Text;
            Thread th = new Thread(new ThreadStart(delegate()
            {
                ParentPage.ContentManager.Session.Put<string>("userName", userName);
                ParentPage.ContentManager.LoadPage<IPage>(new TypeDescriptor("xap://SBPweb.PROvibe.Client.Modules.Default.xap/SBPweb.PROvibe.Client.Modules.Default.dll?class=SBPweb.PROvibe.Client.Modules.Default.MainPage"));
            }));
            th.Start();
        }

        private void Login_ContainerClosing(object sender, CancelEventArgs e)
        {
            e.Cancel = true;
        }

        #endregion

    }

}